<template>
  <div class="tags">
    <ul class="current">
      <li v-for="tag in tags" :key="tag.id"
          :class="{selected: value.indexOf(tag.name)>=0}"
          @click="toggle(tag.name)"
      >{{ tag.name }}
      </li>
    </ul>
    <div class="new">
      <button @click="createTag">新增标签</button>
    </div>
  </div>
</template>

<script lang="ts">
import {Component, Prop} from 'vue-property-decorator';
import {mixins} from 'vue-class-component';
import TagHelper from '@/mixins/TagHelper';

@Component
export default class Tags extends mixins(TagHelper) {
  @Prop(Array) value!: string[];

  //可以选中或取消tag
  toggle(tag: string) {
    const index = this.value.indexOf(tag);
    this.value.splice(index, 1);
    if (index < 0) {
      this.value.push(tag);
    }
    this.$emit('update:value', this.value);
  }
}
</script>

<style lang="scss" scoped>
.tags {
  font-size: 14px;
  padding: 10px 16px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  background-color: white;
  flex-grow: 1;
  overflow-y: auto;

  > .current {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;

    > li {
      $bg: #d9d9d9;
      background-color: $bg;
      $h: 24px;
      height: $h;
      line-height: $h;
      border-radius: ($h/2);
      padding: 0 16px;
      margin-right: 12px;
      margin-top: 4px;

      &.selected {
        background-color: darken($bg, 50%);
        color: #f5f5f5;
      }
    }
  }

  > .new {
    padding-top: 10px;

    button {
      background-color: transparent;
      border: none;
      color: #999;
      border-bottom: 1px solid;
      padding: 0 3px;
    }
  }

}
</style>
